package com.service.impl;

import com.mapper.ContentMapper;
import com.pojo.Content;
import com.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentMapper;


    @Override
    public void insertContent(Content content) {
        contentMapper.insertContent(content);
    }

    @Override
    public void deleteContent(Integer id, Integer userId) {
        contentMapper.deleteContent(id,userId);
    }

    @Override
    public void updateContent(String content, Integer id) {
        contentMapper.updateContent(content,id);

    }

    @Override
    public List<Content> todaylist(String startDate, Integer userId) {
        return contentMapper.findTodoToday(startDate,userId);
    }

    @Override
    public void updateAddress(String address, Integer id) {
        contentMapper.updateAddress(address,id);

    }

    @Override
    public void updateType(String type, Integer id) {
        contentMapper.updateType(type,id);

    }

    @Override
    public void updateStartDate(String startDate, Integer id) {
        contentMapper.updateStartDate(startDate, id);

    }

    @Override
    public void updateEndDate(String endDate, Integer id) {
        contentMapper.updateEndDate(endDate, id);

    }

    @Override
    public void updateDate(String startDate, String endDate, Integer id) {
        contentMapper.updateDate(startDate,endDate,id);
    }

    @Override
    public void updatePartner(String partner, Integer id) {
        contentMapper.updatePartner(partner, id);

    }

    @Override
    public void updateTodo(Content content) {
        contentMapper.updateTodo(content);
    }

    @Override
    public void workDone(Integer id,Integer status) {
        contentMapper.workDone(id,status);
    }

    @Override
    public List<Content> todoList(Integer userId) {
        List<Content> todoList = contentMapper.searchTodo(userId);
        return todoList;
    }

    @Override
    public List<Content> findTodoByDay(Integer userId) {
        List<Content> todoList = contentMapper.findTodoByDay(userId);
        return todoList;
    }

    /*按id查询待办*/
    @Override
    public Content findById(Integer id) {
        return contentMapper.findById(id);
    }

    @Override
    public List<Content> findById2(Integer id) {
        return contentMapper.findById2(id);
    }


    @Override
    public List<Content> findTodoNearSeven(Integer userId) {
        List<Content> todoList = contentMapper.findTodoNearSeven(userId);
        return todoList;
    }

    @Override
    public List<Content> findTodoByMonth(Integer userId) {
        List<Content> todoList = contentMapper.findTodoByMonth(userId);
        return todoList;
    }

    @Override
    public double todoCountByMonth(Integer userId) {
        Double count = contentMapper.todoCount(userId);
        if (count == null){
            count = 0.0;
        }
        return count;
    }

    @Override
    public double doneCountByMonth(Integer userId) {
        Double done = contentMapper.doneCount(userId);
        if (done == null){
            done = 0.0;
        }
        return done;
    }

    @Override
    public double todoCountByDay(Integer userId) {
        Double count  =contentMapper.todoCountByDay(userId);
        if (count == null){
            count = 0.0;
        }
        return count;
    }

    @Override
    public double todoDoneByDay(Integer userId) {
        Double count = contentMapper.todoDoneByDay(userId);
        if (count == null){
            count = 0.0;
        }
        return count;
    }

    @Override
    public double todoCountByWeek(Integer userId) {
        Double count = contentMapper.todoCountByWeek(userId);
        if (count == null){
            count = 0.0;
        }
        return count;
    }

    @Override
    public double todoDoneByWeek(Integer userId) {
        Double count = contentMapper.todoDoneByWeek(userId);
        if (count == null){
            count = 0.0;
        }
        return count;
    }

    @Override
    public Map<String, Double> mapPercent(Integer userId) {
        double count = this.todoCountByMonth(userId);
        double done = this.doneCountByMonth(userId);
        double countDay = this.todoCountByDay(userId);
        double doneDay = this.todoDoneByDay(userId);
        double countWeek = this.todoCountByWeek(userId);
        double doneWeek  = this.todoDoneByWeek(userId);
        Integer countStudy = countByStudy(userId);
        Integer countWork  = countByWork(userId);
        Integer countLife = countByLife(userId);
        Integer countEnter = countByEntertainment(userId);

//        保留两位小数
        double percentMonth = (Math.round(done*100/count)/100.0);
        double percentDay = (Math.round(doneDay*100/countDay)/100.0);
        double percentWeek = (Math.round(doneWeek*100/countWeek)/100.0);

        Map<String,Double> mapPercent  = new HashMap<>();
        mapPercent.put("countDay",countDay);
        mapPercent.put("countWeek",countWeek);
        mapPercent.put("countMonth",count);
        mapPercent.put("doneDay",doneDay);
        mapPercent.put("doneWeek",doneWeek);
        mapPercent.put("doneMonth",done);
        mapPercent.put("percentDay",percentDay);
        mapPercent.put("percentWeek",percentWeek);
        mapPercent.put("percentMonth",percentMonth);
        mapPercent.put("countStudy", Double.valueOf(countStudy));
        mapPercent.put("countWork", Double.valueOf(countWork));
        mapPercent.put("countLife", Double.valueOf(countLife));
        mapPercent.put("countEnter", Double.valueOf(countEnter));
        return mapPercent;
    }

    @Override
    public List<Content> findTodoByWantMonth(String wantMonth, Integer userId) {
        List<Content> todoList = contentMapper.findTodoWantMonth(wantMonth,userId);
        return todoList;
    }


    @Override
    public List<Content> findByAnyWhat(Content content) {
        List<Content> todoList = contentMapper.findByAnyWhat(content);
        return todoList;
    }

    @Override
    public Integer countByStudy(Integer userId) {
        Integer count = contentMapper.countByStudy(userId);
        if (count == null){
            count=0;
        }
        return count;
    }

    @Override
    public Integer countByLife(Integer userId) {
        Integer count = contentMapper.countByLife(userId);
        if (count == null){
            count = 0;
        }
        return count;
    }

    @Override
    public Integer countByWork(Integer userId) {
        Integer count = contentMapper.countByWork(userId);
        if (count == null){
            count = 0;
        }
        return count;
    }

    @Override
    public Integer countByEntertainment(Integer userId) {
        Integer count = contentMapper.countByEntertainment(userId);
        if (count == null){
            count = 0;
        }
        return count;
    }

    @Override
    public List<Content> findAny(String keyWords, Integer userId) {
        List<Content> list = contentMapper.findAny("%"+keyWords+"%",userId);
        return list;
    }

    @Override
    public Double countByWhatMonth(Content content) {
        Double count = contentMapper.countByWhatMonth(content);
        if (count  == null){
            count=0.0;
        }
        return count;
    }

    @Override
    public Double doneByWhatMonth(Content content) {
        Double count  = contentMapper.doneByWhatMonth(content);
        if (count == null){
            count=0.0;
        }
        return count;
    }

    @Override
    public Map<String, Object> mapMonthPercent(Content content) {
        double count = countByWhatMonth(content);
        double done = doneByWhatMonth(content);
        double percent = (Math.round(done*100/count)/100.0);
        String startDate = content.getStartDate();
        Map<String,Object> map = new HashMap<>();
        map.put("countWhatMonth",count);
        map.put("doneWhatMonth",done);
        map.put("percent",percent);
        map.put("startDate",startDate);
        return map;
    }
}
